package com.cljh.controller;

import com.cljh.bean.Article;
import com.cljh.config.AuthAccess;
import com.cljh.controller.page.Code;
import com.cljh.controller.page.PageBean;
import com.cljh.controller.page.Result;
import com.cljh.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/articles")
public class ArticleController {
    @Autowired
    ArticleService articleService;
    /**
     * 分页查询
     * @param pn 当前页面
     * @param size 每页显示数量
     * @param article 文章类
     * @return Result
     */
    @AuthAccess
    @PostMapping("/{pn}/{size}")
    public Result selectAllByCondition(@PathVariable(value = "pn") Integer pn, @PathVariable(value = "size")Integer size, @RequestBody Article article){
        PageBean<Article> articlePageBean = articleService.selectAllByCondition(pn, size, article);

        Integer code=articlePageBean !=null ? Code.GET_OK:Code.GET_ERR;
        String msg=articlePageBean!=null ? "查询到信息":"未查询到信息";
        return new Result(code,articlePageBean,msg);
    }
    /**
     *添加
     * @param article 文章类
     * @return
     */
    @PostMapping
    public Result save(@RequestBody Article article){
        boolean save = articleService.save(article);
        Integer code=save ? Code.SAVE_OK:Code.SAVE_ERR;
        String msg=save ? "添加成功":"添加失败";
        return new Result(code,save,msg);
    }

    /**
     * id删除
     * @param id
     * @return Result
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        boolean delete = articleService.deleteById(id);

        Integer code=delete ? Code.DELETE_OK:Code.DELETE_ERR;
        String msg=delete ? "删除一个成功":"删除一个失败";
        return new Result(code,delete,msg);
    }

    /**
     * 更新
     * @param article 文章类
     * @return Result
     */
    @PutMapping
    public Result update(@RequestBody Article article){
        boolean update = articleService.update(article);

        Integer code=update ? Code.UPDATE_OK:Code.UPDATE_ERR;
        String msg=update ? "修改成功":"修改失败";
        return new Result(code,update,msg);
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @DeleteMapping("/deleteIds")
    public Result deleteByIds(@RequestBody List<Integer> ids){
        boolean b = articleService.deleteByIds(ids);

        Integer code=b ? Code.DELETE_OK:Code.DELETE_ERR;
        String msg=b ? "删除成功":"删除失败";
        return new Result(code,b,msg);
    }

    /**
     * id查询
     * @param id
     * @return
     */
    @AuthAccess
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        Article article = articleService.selectById(id);
        Integer code=article !=null ? Code.GET_OK:Code.GET_ERR;
        String msg=article!=null ? "查询到信息":"未查询到信息";
        return new Result(code,article,msg);
    }

    @AuthAccess
    @GetMapping("/admin/{id}")
    public Result selectByAdmin(@PathVariable Integer id){
        List<Article> article = articleService.selectByAdmin(id);
        Integer code=article !=null ? Code.GET_OK:Code.GET_ERR;
        String msg=article!=null ? "查询到信息":"未查询到信息";
        return new Result(code,article,msg);
    }
    @AuthAccess
    @GetMapping("/all")
    public Result ArticleList(){
        List<Article> article = articleService.ArticleList();
        Integer code=article !=null ? Code.GET_OK:Code.GET_ERR;
        String msg=article!=null ? "查询到信息":"未查询到信息";
        return new Result(code,article,msg);
    }
}
